using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Lottery.Data;

namespace Lottery
{
	/// <summary>
	/// Summary description for frmCombinationManager.
	/// </summary>
	public class frmCombinationManager : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox rtbStatus;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.DataGrid grdCalculatedResults;
		private System.Windows.Forms.Button btnAnalyze;
		private System.Windows.Forms.Button btnSelect;
		private System.Windows.Forms.Label lblSelectedCount;
		private System.Windows.Forms.Splitter splCMStatus;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private void AddStatusString(string str)
		{
			rtbStatus.AppendText(str + "\n");
		}

		public frmCombinationManager()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.rtbStatus = new System.Windows.Forms.RichTextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.lblSelectedCount = new System.Windows.Forms.Label();
			this.btnSelect = new System.Windows.Forms.Button();
			this.btnAnalyze = new System.Windows.Forms.Button();
			this.grdCalculatedResults = new System.Windows.Forms.DataGrid();
			this.splCMStatus = new System.Windows.Forms.Splitter();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.grdCalculatedResults)).BeginInit();
			this.SuspendLayout();
			// 
			// rtbStatus
			// 
			this.rtbStatus.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.rtbStatus.Location = new System.Drawing.Point(0, 373);
			this.rtbStatus.Name = "rtbStatus";
			this.rtbStatus.Size = new System.Drawing.Size(768, 96);
			this.rtbStatus.TabIndex = 2;
			this.rtbStatus.Text = "";
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.lblSelectedCount,
																				 this.btnSelect,
																				 this.btnAnalyze,
																				 this.grdCalculatedResults});
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(768, 373);
			this.panel1.TabIndex = 3;
			// 
			// lblSelectedCount
			// 
			this.lblSelectedCount.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.lblSelectedCount.Location = new System.Drawing.Point(352, 40);
			this.lblSelectedCount.Name = "lblSelectedCount";
			this.lblSelectedCount.Size = new System.Drawing.Size(72, 23);
			this.lblSelectedCount.TabIndex = 5;
			this.lblSelectedCount.Text = "Selected: 0";
			this.lblSelectedCount.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// btnSelect
			// 
			this.btnSelect.Location = new System.Drawing.Point(352, 72);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.TabIndex = 4;
			this.btnSelect.Text = "Select";
			this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
			// 
			// btnAnalyze
			// 
			this.btnAnalyze.Location = new System.Drawing.Point(352, 8);
			this.btnAnalyze.Name = "btnAnalyze";
			this.btnAnalyze.TabIndex = 3;
			this.btnAnalyze.Text = "Analyze";
			this.btnAnalyze.Click += new System.EventHandler(this.btnAnalyze_Click);
			// 
			// grdCalculatedResults
			// 
			this.grdCalculatedResults.BackgroundColor = System.Drawing.SystemColors.Window;
			this.grdCalculatedResults.DataMember = "";
			this.grdCalculatedResults.Dock = System.Windows.Forms.DockStyle.Left;
			this.grdCalculatedResults.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.grdCalculatedResults.Name = "grdCalculatedResults";
			this.grdCalculatedResults.ReadOnly = true;
			this.grdCalculatedResults.Size = new System.Drawing.Size(344, 373);
			this.grdCalculatedResults.TabIndex = 2;
			// 
			// splCMStatus
			// 
			this.splCMStatus.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.splCMStatus.Location = new System.Drawing.Point(0, 370);
			this.splCMStatus.Name = "splCMStatus";
			this.splCMStatus.Size = new System.Drawing.Size(768, 3);
			this.splCMStatus.TabIndex = 4;
			this.splCMStatus.TabStop = false;
			// 
			// frmCombinationManager
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(768, 469);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.splCMStatus,
																		  this.panel1,
																		  this.rtbStatus});
			this.Name = "frmCombinationManager";
			this.Text = "Combination Manager";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.grdCalculatedResults)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void btnAnalyze_Click(object sender, System.EventArgs e)
		{
			AddStatusString("Analyzing started ...");
			int[,] InitArray = new int[Config.PDC, Config.NPDC];
			ArrayList ChoosedNumbers = new ArrayList();
			string Query = "select d.Number, d.Date, c.first, c.second, c.third, c.fourth, c.fifth, c.sixth " 
				+ " from Drawing d, Combination c "
				+ " where c.ID = d.CombinationID and d.Number < 440 order by d.Number desc";

			AddStatusString("Reading data ...");

			DataTable Drawings = SQLHelper.FillDataSet(Query).Tables[0];
	
			AddStatusString(String.Format("Analyzing drawing from {0} to {1}", Drawings.Rows[0][0].ToString(), Drawings.Rows[Drawings.Rows.Count-1][0].ToString()));

			//Data initialization
			for(int i=0; i<Config.PDC; i++)
				for(int j=0; j<Config.NPDC; j++)
					InitArray[i,j] = Convert.ToInt32(Drawings.Rows[i][j+2]);

			//Choose the numbers from last Drawings
			for(int i=0; i<Config.PDC; i++)
				for(int j=0; j<Config.NPDC; j++)
					if(!ChoosedNumbers.Contains(InitArray[i,j]))
						ChoosedNumbers.Add(InitArray[i,j]);
			
			ChoosedNumbers.Sort();

			//Calculate falling count
			int[] FallCount = new int[49];
			for(int i=0; i<Config.PDC; i++)
				for(int j=0; j<Config.NPDC; j++)
					FallCount[InitArray[i,j]]++;
			AddStatusString("Calculating started ...");
			
			Calculated.ChoosedNumbersDataTable dt  = new Calculated.ChoosedNumbersDataTable();

			for(int i=0; i<ChoosedNumbers.Count; i++)
			{
				Calculated.ChoosedNumbersRow dr = dt.NewChoosedNumbersRow();
				dr.Number = Convert.ToInt32(ChoosedNumbers[i]);
				dr.FallCount = FallCount[Convert.ToInt32(ChoosedNumbers[i])];
				dt.AddChoosedNumbersRow(dr);
			}

			grdCalculatedResults.DataSource = new DataView(dt, "FallCount < 4", "FallCount Desc, Number", DataViewRowState.CurrentRows);
			AddStatusString("Analyzing finished ...");

			/*for(int i=0; i < ((DataView)grdCalculatedResults.DataSource).Count; i++)
				if((int)grdCalculatedResults[i,1] == 2)
					grdCalculatedResults[i,0]*/
		}

		private void btnSelect_Click(object sender, System.EventArgs e)
		{
			int [] InitArray = new int[9];
			int j=0;
			for(int i=0; i < ((DataView)grdCalculatedResults.DataSource).Count; i++)
			{
				if(grdCalculatedResults.IsSelected(i))
				{
					if(j > 8) 
						throw new Exception("Invalid count of selected rows.");
					InitArray[j] = Convert.ToInt32(grdCalculatedResults[i,0].ToString());
					j++;
				}
			}
			Array.Sort(InitArray);
			int[,] ReslutArray = CalculationUtils.CombinationCalculation.KS9C7(InitArray);
			for(int i=0; i<7; i++)
			{
				string values = string.Empty;
				for(int k=0; k<6; k++)
				{
					values += ReslutArray[i,k].ToString();
					if(k<5) values += ", ";
				}
				AddStatusString(values);
			}
		}
	}
}
